package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioGroup;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.fragment.AllFragment;
import com.banshuibao.bsb_android.fragment.IncomeFragment;
import com.banshuibao.bsb_android.fragment.PayFragment;

public class DealActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private ImageView back;
    private RadioGroup radioGroup;
    private FrameLayout frameLayout;
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private AllFragment allFragment;
    private IncomeFragment incomeFragment;
    private PayFragment payFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deal);
        manager = getSupportFragmentManager();
        initView();
        showFragment(R.id.rb_all);
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back_deal);
        radioGroup = (RadioGroup) findViewById(R.id.rg);
        frameLayout = (FrameLayout) findViewById(R.id.deal_frame);
        back.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_deal:
                finish();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_all:
                showFragment(R.id.rb_all);
                break;
            case R.id.rb_goin:
                showFragment(R.id.rb_goin);
                break;
            case R.id.rb_pay:
                showFragment(R.id.rb_pay);
                break;
        }
    }

    private void showFragment(int i) {
        transaction = manager.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case R.id.rb_all:
                if (allFragment == null) {
                    allFragment = new AllFragment();
                    transaction.add(R.id.deal_frame, allFragment, "all");
                } else {
                    transaction.show(allFragment);
                }
                break;
            case R.id.rb_goin:
                if (incomeFragment == null) {
                    incomeFragment = new IncomeFragment();
                    transaction.add(R.id.deal_frame, incomeFragment, "income");
                } else {
                    transaction.show(incomeFragment);
                }
                break;
            case R.id.rb_pay:
                if (payFragment == null) {
                    payFragment = new PayFragment();
                    transaction.add(R.id.deal_frame, payFragment, "pay");
                } else {
                    transaction.show(payFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction ft) {
        if (allFragment != null) {
            ft.hide(allFragment);
        }
        if (incomeFragment != null) {
            ft.hide(incomeFragment);
        }
        if (payFragment != null) {
            ft.hide(payFragment);
        }
    }
}
